﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Trade.Journal.Controls
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:Trade.Journal"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:Trade.Journal;assembly=Trade.Journal"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:StringBox/>
    ///
    /// </summary>
    public class StringBox : ItemsControl
    {
        private TextBox PART_Text;
        private ListView PART_List;

        public Style ListViewStyle
        {
            get { return (Style)GetValue(ListViewStyleProperty); }
            set { SetValue(ListViewStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ListViewStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ListViewStyleProperty =
            DependencyProperty.Register("ListViewStyle", typeof(Style), typeof(StringBox), new PropertyMetadata(null));




        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set { SetValue(IsEditableProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditableProperty =
            DependencyProperty.Register("IsEditable", typeof(bool), typeof(StringBox), new PropertyMetadata(true));





        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(StringBox), new PropertyMetadata(Orientation.Horizontal));




        static StringBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(StringBox), new FrameworkPropertyMetadata(typeof(StringBox)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            PART_Text = Template.FindName("PART_Text", this) as TextBox;
            PART_List = Template.FindName("PART_List", this) as ListView;

            CommandBindings.Add(new CommandBinding(Commands.DeleteCommand, new ExecutedRoutedEventHandler(DeleteCommand_Executed)));
        }


        private void DeleteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Parameter is string str)
            {
                if (PART_List?.ItemsSource is ICollection<string> s)
                    s.Remove(str);

                if (!(ItemsSource is INotifyCollectionChanged))
                    PART_List.Items.Refresh();
            }
        }

        protected override void OnPreviewKeyUp(KeyEventArgs e)
        {
            if (e.Key == Key.Return && PART_Text != null)
            {
                if (!string.IsNullOrWhiteSpace(PART_Text.Text) && ItemsSource is ICollection<string> s)
                {
                    s.Add(PART_Text.Text);

                    if (PART_List != null)
                    {
                        if (!(ItemsSource is INotifyCollectionChanged))
                            PART_List.Items.Refresh();
                        PART_List.ScrollIntoView(PART_List.Items.GetItemAt(PART_List.Items.Count - 1));
                    }
                }
                PART_Text.Text = string.Empty;
                if (!(ItemsSource is INotifyCollectionChanged))
                    PART_List.Items.Refresh();
            }
            base.OnPreviewKeyUp(e);
        }
    }
}
